﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reddit;
using Reddit.Data;
using System.Net;
using Reddit.JsonParser;
using Reddit.Api.Parameters;
using System.Collections;

namespace RedditConsole
{
    class Program
    {
        static void PrintListing(Object listingContainerObj, string childField)
        {
            var listingContainer = listingContainerObj as Dictionary<string, object>;
            Console.WriteLine("Main Kind: {0}", listingContainer["kind"]);
            var listing = listingContainer["data"] as Dictionary<string, object>;
            var children = listing["children"] as Object[];
            foreach (Dictionary<string, object> childContainer in children.Select(x => x as Dictionary<string, object>))
            {
                var child = childContainer["data"] as Dictionary<string, object>;
                Console.WriteLine("\tChild Kind: {0}, {1}: {2}",
                    childContainer["kind"],
                    childField,
                    child[childField]);
            }
        }
        static void PrintTitles(Object reddits)
        {
            PrintListing(reddits, "title");
        }
        static void PrintStoryComments(Object results)
        {
            var storyComments = results as Object[];
            Console.WriteLine("Printing story title");
            PrintTitles(storyComments[0]);
            Console.WriteLine("Printing comment authors");
            PrintListing(storyComments[1], "author");
        }
        class ActionDescription
        {
            public string desc { get; set; }
            public Action action { get; set; }
        }
        static void Main(string[] args)
        {
            Reddit.RedditApi redditApi = new Reddit.RedditApi();
            Reddit.HttpRedditApi httpApi = new Reddit.HttpRedditApi();
            Reddit.UrlRedditApi urlApi = new Reddit.UrlRedditApi();
            ActionDescription[] actions = new ActionDescription[]
            {
                new ActionDescription()
                {
                    desc = "UrlApi: GetReddits",
                    action = () => Console.WriteLine(urlApi.Subreddit.GetSubreddits(null))
                },
                new ActionDescription()
                {
                    desc = "HttpApi: GetReddits",
                    action = () => Console.WriteLine(httpApi.Subreddit.GetSubreddits(null))
                },
                new ActionDescription()
                {
                    desc = "RedditApi: GetReddits",
                    action = () => PrintTitles(redditApi.Subreddit.GetSubreddits(null))
                },
                new ActionDescription()
                {
                    desc = "RedditApi: GetStoriesHot Pics subreddit",
                    action = () => PrintTitles(redditApi.Story.GetStoriesHot(new GetStoriesHotParameters()
                    {
                        subreddit = "pics"
                    }))
                },
                new ActionDescription()
                {
                    desc = "RedditApi: GetStoryComments",
                    action = () => PrintStoryComments(redditApi.Story.GetStoryComments(
                        new GetStoryCommentsParameters("programming", "1jhma")))
                },
                new ActionDescription()
                {
                    desc = "RedditApi: Get Invalid Story",
                    action = () => PrintStoryComments(redditApi.Story.GetStoryComments(
                        new GetStoryCommentsParameters("programming", "!@#$")))
                },
            };

            while (true)
            {
                int menuOption;
                for (int i = 0; i < actions.Length; i++)
                {
                    Console.WriteLine("{0}: {1}", i, actions[i].desc);
                }
                Console.Write("\nChoose an option: ");

                var result = Console.ReadLine();
                if (Int32.TryParse(result, out menuOption))
                {
                    if (menuOption >= 0 && menuOption < actions.Length)
                    {
                        Console.WriteLine("\nRunning: {0}\n", actions[menuOption].desc);
                        try
                        {
                            actions[menuOption].action();
                        }
                        catch (WebException we)
                        {
                            //404 etc
                            Console.WriteLine(we.ToString());
                        }
                        catch (JsonParseException jpe)
                        {
                            /* Would happen if the server decided to send something invalid, but not an error */
                            Console.WriteLine(jpe.ToString());
                        }
                        catch (Exception e)
                        {
                            //?
                            Console.WriteLine(e.ToString());
                        }
                    }
                }
                Console.WriteLine();
            }

        }
    }
}
